import { Injectable } from '@nestjs/common';
import { readFileList } from './utils/readFile'
import { serverPort, serverUrl } from './../server.config'
import { readFile } from 'fs/promises';
import { resolve } from 'path';

const getFileMapList = async () => {

    const files = await readFileList();
    const fileMap = files.map((fileObj) => {
        Object.entries(fileObj).map(([key, value]) => {
            value.map((path, i) => {

                fileObj[key][i] = `${serverUrl}:${serverPort}/files/courses/${key}/${path}`
            })
        })
        return fileObj;

    });

    return fileMap;

}

const getFileContent = async (path: string) => {
    const file = await readFile(resolve(__dirname, `../../static/courses/${path}`));
    return file.toString();
}




@Injectable()
export class AppService {

    getFileMapList() {

        return getFileMapList();

    }

    getFileContent(name1, name2) {
        return getFileContent(name1 + '/' + name2);
    }


}

